package com.angel.core.payUtil;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.codec.digest.DigestUtils;
import org.csource.common.NameValuePair;

import com.angel.core.Constants;
import com.angel.core.util.PropertyConfUtil;
/** 
* @author 作者姓名  cheng
* @version 
* 创建时间：2016年12月3日 下午3:00:32 
* 类说明  维信支付
*/
public class WeiXinPay {
	/** 统一下单接口链接 */
	public static final String url="https://api.mch.weixin.qq.com/pay/unifiedorder";

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static String submitOrder(double realPayPrice, String tradeNo, String ip)
			throws UnsupportedEncodingException {
		int realpayPrice = (int) (realPayPrice * 100);
		List<NameValuePair> nvps = new ArrayList();
		// 调用统一下单接口必需传的参数,可以查看微信支付统一下单接口api查看每个参数的意思
		String appid = PropertyConfUtil.getContextProperty(Constants.WEIXIN_APPID);
		String mch_id = PropertyConfUtil.getContextProperty(Constants.WEIXIN_MCHID);
		String key = PropertyConfUtil.getContextProperty(Constants.WEIXIN_KEY);
//		String appid = "wxf245ee13555f1e61";
//		String mch_id = "1418837002";
//		String key  = "jrSpbVFQMQsjjCAeaRMnkIqjHxkHmDsC";
		nvps.add(new NameValuePair("appid", appid));
		nvps.add(new NameValuePair("body", "body"));
		nvps.add(new NameValuePair("mch_id", mch_id));
		nvps.add(new NameValuePair("nonce_str", UUID.randomUUID().toString()
				.replace("-", "")));
		nvps.add(new NameValuePair("notify_url","http://www.weixin.qq.com/wxpay/pay.php")); //回调地址需要根据实际项目做修改
		nvps.add(new NameValuePair("out_trade_no", tradeNo));
		nvps.add(new NameValuePair("spbill_create_ip", ip));//ip地址需要根据实际项目做修改
		nvps.add(new NameValuePair("total_fee", realpayPrice + ""));
		nvps.add(new NameValuePair("trade_type", "APP"));
		StringBuffer sb = new StringBuffer();

		for (NameValuePair nvp : nvps) {
			sb.append(nvp.getName() + "=" + nvp.getValue() + "&");
		}
		String signA = sb.toString(); // 根据签名格式组装数据，详见微信支付api
		String stringSignTemp = signA + "key=" + key; // 根据签名格式组装数据，详见微信支付api
		String sign = DigestUtils.md5Hex(
				getContentBytes(stringSignTemp, "UTF-8")).toUpperCase(); // 把组装好的签名数据md5之后字母都转换为大写，详见微信支付api
		System.out.println("sign=" + sign);
		nvps.add(new NameValuePair("sign", sign)); // 把签名后的数据组装成参数
		TenpayHttpClient httpClient = new TenpayHttpClient();
		httpClient.setReqContent(url);
		String resContent = "";
		if (httpClient.callHttpPost(url, toXml(nvps))) {
			resContent = httpClient.getResContent();
			String result = new String(resContent.getBytes("GBK"), "UTF-8");
			System.out.println("请求返回的结果=" + result);
			return result;
		}
		return null;
	}

	// 转换成xml格式
	private static String toXml(List<NameValuePair> params) {
		StringBuilder sb = new StringBuilder();
		sb.append("<xml>");
		for (int i = 0; i < params.size(); i++) {
			sb.append("<" + params.get(i).getName() + ">");
			sb.append((params.get(i)).getValue());
			sb.append("</" + params.get(i).getName() + ">");
		}
		sb.append("</xml>");
		return sb.toString();
	}

	// 编码转换
	public static byte[] getContentBytes(String content, String charset) {
		if (charset == null || "".equals(charset)) {
			return content.getBytes();
		}
		try {
			return content.getBytes(charset);
		} catch (UnsupportedEncodingException e) {
			throw new RuntimeException("MD5签名过程中出现错误,指定的编码集不对,您目前指定的编码集是:"
					+ charset);
		}
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		WeiXinPay wx = new WeiXinPay();
//		wx.submitOrder(10.00, 1, "127.0.0.1");
	}
}